//
//  ViewController.m
//  TableView_Xib
//
//  Created by 幸福已倒带 on 16/5/22.
//  Copyright © 2016年 幸福已倒带. All rights reserved.
//

#import "ViewController.h"
#import "CellOfNews.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (retain, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, retain) NSDictionary *dicofdata;

@property (nonatomic, retain) NSArray *arrofdata;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createconfig];
    [self readJSON];

    UINib *nib = [UINib nibWithNibName:@"CellOfNews" bundle:[NSBundle mainBundle]];
    [self.tableview registerNib:nib forCellReuseIdentifier:@"pool"];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
}

- (void)createconfig{

self.navigationItem.title = @"活动列表";


}

- (void)readJSON {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ActivityList" ofType:@".txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    self.dicofdata = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    self.arrofdata = [self.dicofdata objectForKey:@"events"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return self.arrofdata.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CellOfNews *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
   NSDictionary *dic = [self.arrofdata objectAtIndex:indexPath.row];
    cell.lableOfTitle.text = dic[@"title"];
    
    
   
    NSString *strofbegin = [dic[@"begin_time"] substringFromIndex:5];
    NSString *strofend = [dic[@"end_time"] substringFromIndex:5];
    NSString *str = @"--";
    cell.lableOfTime.text = [[strofbegin stringByAppendingString:str] stringByAppendingString:strofend];
    cell.lableOfLocation.text = dic[@"address"];
    cell.lableOfType.text = dic[@"category_name"];
    NSString *strofinterest = @"感兴趣:";
    NSString *strofjoin = @"参加:";
    cell.lableOfInterested.text = [strofinterest stringByAppendingString:[NSString stringWithFormat:@"%@", dic[@"wisher_count"]]];
    cell.lableOfJoin.text = [strofjoin stringByAppendingString:[NSString stringWithFormat:@"%@", dic[@"participant_count"]]];
    
    
    NSURL *nsurlofimage = [NSURL URLWithString:dic[@"image"]];
    NSData *dataofimage = [NSData dataWithContentsOfURL:nsurlofimage];
    cell.imageOfBig.image = [UIImage imageWithData:dataofimage];

    
    cell.imageOfTime.image = [UIImage imageNamed:@"Unknown.png"];
    cell.imageOfLocation.image = [UIImage imageNamed:@"Unknown.png"];
    cell.imageOfType.image = [UIImage imageNamed:@"Unknown.png"];

    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 200;
    
    
    
    
}



- (void)dealloc {
    [_tableview release];
    [super dealloc];
}
@end
